﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BUS;
using DAO;

namespace FDeal.WUC
{
    public partial class WUCDonHang : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDonHang();
            }
        }
        private void LoadDonHang()
        {
            DonHangBUS donHangBUS = new DonHangBUS();
            gvDonHang.DataSource = donHangBUS.LietKe();
            gvDonHang.DataBind();
        }
        public static int idong;
        public static String iddonhang;
        protected void gvDonHang_RowEditing(object sender, GridViewEditEventArgs e)
        {
            idong = e.NewEditIndex;

            iddonhang = ((Label)gvDonHang.Rows[idong].Cells[0].FindControl("lb1")).Text;
            gvDonHang.EditIndex = idong;

            LoadDonHang();
        }
        protected void gvDonHang_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDonHang.EditIndex = -1;
            LoadDonHang();
        }
        protected void gvDonHang_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (e.NewValues == e.OldValues)
                e.Cancel = true;
            else
            {
                DonHangBUS dhBUS = new DonHangBUS();
                DONHANG donhang = dhBUS.ChiTiet(iddonhang);
                TextBox t = ((TextBox)gvDonHang.Rows[e.RowIndex].Cells[1].FindControl("tb2"));

                donhang.IDDEAL = ((TextBox)gvDonHang.Rows[e.RowIndex].Cells[1].FindControl("tb2")).Text.ToString();
                donhang.SOLUONG = int.Parse(((TextBox)gvDonHang.Rows[e.RowIndex].Cells[2].FindControl("tbSoLuong")).Text.ToString());
                donhang.TONGGIA = int.Parse(((TextBox)gvDonHang.Rows[e.RowIndex].Cells[3].FindControl("tbTongGia")).Text.ToString());
                donhang.DIACHINHAN = ((TextBox)gvDonHang.Rows[e.RowIndex].Cells[4].FindControl("tbDiaChiNhan")).Text.ToString();
                donhang.HINHTHUCNHANHANG = ((TextBox)gvDonHang.Rows[e.RowIndex].Cells[5].FindControl("tbHinhThucNhanHang")).Text.ToString();
                donhang.TRANGTHAI = ((TextBox)gvDonHang.Rows[e.RowIndex].Cells[6].FindControl("tbTrangThai")).Text.ToString();
                dhBUS.Sua(donhang);
                gvDonHang.EditIndex = -1;
                LoadDonHang();
            }
        }
        protected void gvDonHang_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            DonHangBUS dhBUS = new DonHangBUS();
            iddonhang = ((Label)gvDonHang.Rows[e.RowIndex].Cells[0].FindControl("lb1")).Text;
            DONHANG donhang = dhBUS.ChiTiet(iddonhang);
            dhBUS.Xoa(donhang);
            gvDonHang.EditIndex = -1;
            LoadDonHang();
        }
    }
}